package jw.guli.eduservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import jw.guli.commonutils.R;
import jw.guli.eduservice.entity.EduTeacher;
import jw.guli.eduservice.entity.vo.TeacherQuery;
import jw.guli.eduservice.service.EduTeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author jiangw
 * @since 2021-01-26
 */
@Api(description = "讲师管理")
@RestController
@RequestMapping("/eduservice/edu-teacher")
//@CrossOrigin
public class EduTeacherController {
    @Autowired
    private EduTeacherService teacherService;

    @ApiOperation(value = "所有讲师列表")
    @GetMapping("/findAll")
    public R list() {
        List<EduTeacher> list = teacherService.list(null);
//        try {
//            int i=1/0;
//        }catch (Exception e){
//            throw new GuliException(2,"执行自定义异常处理");
//        }
        return R.ok().data("EduTeacherList",list);
    }

    @ApiOperation(value = "根据ID删除讲师")
    @DeleteMapping("/{id}")
    public R removeById(
            @ApiParam(name = "id", value = "讲师ID", required = true)
            @PathVariable String id) {
        boolean flag = teacherService.removeById(id);
        return flag?R.ok():R.error();
    }

    @ApiOperation("分页查询讲师")
    @GetMapping("/pageTeacher/{current}/{limit}")
    public R PageTeacher(
            @ApiParam(name = "current", value = "当前页", required = true)
            @PathVariable Long current,
            @ApiParam(name = "limit", value = "每页数据条数", required = true)
            @PathVariable Long limit){
        Page<EduTeacher> page = new Page<EduTeacher>(current, limit);
        IPage<EduTeacher> pageTeacher = teacherService.page(page, null);
        long total = pageTeacher.getTotal();
        List<EduTeacher> records = page.getRecords();
        HashMap<Object, Object> map = new HashMap<>();
        map.put("total",total);
        map.put("records",records);
        return R.ok().data("map",map);
    }
    @ApiOperation("分页条件查询")
    @PostMapping("/pageTeacherCondition/{current}/{limit}")
    public R PageTeacherCondition(
            @ApiParam(name = "current", value = "当前页", required = true)
            @PathVariable Long current,
            @ApiParam(name = "limit", value = "每页数据条数", required = true)
            @PathVariable Long limit,
            @ApiParam(name = "teacherQuery", value = "查询条件", required = false)
            @RequestBody(required = false) TeacherQuery teacherQuery){
        Page<EduTeacher> page = new Page<EduTeacher>(current, limit);
        QueryWrapper<EduTeacher> queryWrapper = new QueryWrapper<>();
        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();
        if (!StringUtils.isEmpty(name)) {
            queryWrapper.like("name", name);
        }
        if (!StringUtils.isEmpty(level) ) {
            queryWrapper.eq("level", level);
        }
        if (!StringUtils.isEmpty(begin)) {
            queryWrapper.ge("gmt_create", begin);
        }
        if (!StringUtils.isEmpty(end)) {
            queryWrapper.le("gmt_create", end);
        }
        queryWrapper.orderByDesc("gmt_create");
        IPage<EduTeacher> pageTeacher = teacherService.page(page, queryWrapper);
        long total = pageTeacher.getTotal();
        List<EduTeacher> records = page.getRecords();
        HashMap<Object, Object> map = new HashMap<>();
        map.put("total",total);
        map.put("records",records);
        return R.ok().data("map",map);
    }
    @ApiOperation("添加讲师")
    @PostMapping("/addTeacher")
    public R addTeacher(
            @ApiParam(name = "eduTeacher", value = "讲师信息")
            @RequestBody EduTeacher eduTeacher) {
        boolean flag = teacherService.save(eduTeacher);
        return flag?R.ok():R.error();
    }
    @ApiOperation(value = "根据ID查询讲师")
    @GetMapping("/getTeacher/{id}")
    public R getTeacher(
            @ApiParam(name = "id", value = "讲师ID")
            @PathVariable String id) {
        EduTeacher eduTeacher = teacherService.getById(id);
        return R.ok().data("teacher",eduTeacher);
    }
    @ApiOperation(value = "修改讲师")
    @PostMapping("/updateTeacher")
    public R updateTeacher(
            @ApiParam(name = "eduTeacher", value = "讲师信息")
            @RequestBody EduTeacher eduTeacher) {
        boolean flag = teacherService.updateById(eduTeacher);
        return flag?R.ok():R.error();
    }
}
